// by Xeno
#define THIS_FILE "x_weaponcargor_a3.sqf"
#include "x_setup.sqf"
private "_vec";
PARAMS_1(_vec);

#define __awc(wtype,wcount) _vec addWeaponCargo [#wtype,wcount];
#define __amc(mtype,mcount) _vec addMagazineCargo [#mtype,mcount];
#define __aic(mtype,mcount) _vec addItemCargo [#mtype,mcount];

clearMagazineCargo _vec;
clearWeaponCargo _vec;
clearItemCargo _vec;

if (isNil "x_ranked_weapons") then {
	_x_ranked_weapons_w = [
		[
			// private rifles
			[
				["arifle_MX_F",1]
			],
			// corporal rifles (gets added to private rifles)
			[
				["arifle_MX_GL_F",1], ["arifle_MXM_F",1], ["arifle_Mk20_F",1]
			],
			// sergeant rifles (gets added to corporal and private rifles)
			[
				["arifle_SDAR_F",1], ["arifle_MX_SW_F",1], ["arifle_MXC_F",1], ["arifle_Mk20C_F",1]
			],
			// lieutenant rifles (gets added to...)
			[
				["arifle_TRG21_F",1],["arifle_TRG21_GL_F",1], ["arifle_Mk20_GL_F",1]
			],
			// captain rifles (gets added...)
			[
				["arifle_TRG20_F",1]
			],
			// major rifles (gets...)
			[
				["srifle_EBR_F",1]
			],
			// colonel rifles (...)
			[
			]
		],
		[
			// private sniper rifles
			[
			],
			// corporal sniper rifles
			[
			],
			// sergeant sniper rifles
			[
				["srifle_EBR_F",1]
			],
			// lieutenant sniper rifles
			[
			],
			// captain sniper rifles
			[
			],
			// major sniper rifles
			[
			],
			// colonel sniper rifles
			[
				["srifle_LRR_F",1]
			]
		],
		[
			// private MG
			[
			],
			// corporal MG
			[
				["LMG_Mk200_F",1]
			],
			// sergeant MG
			[
				["SMG_01_F",1]
			],
			// lieutenant MG
			[
			],
			// captain MG
			[
				["SMG_02_F",1]
			],
			// major MG
			[
			],
			// colonel MG
			[
			]
		],
		[
			// private launchers
			[
				["launch_NLAW_F",1], ["launch_Titan_F",1]
			],
			// corporal launchers
			[
				["launch_RPG32_F",1]
			],
			// sergeant launchers
			[
			],
			// lieutenant launchers
			[
			],
			// capain launchers
			[
			],
			// major launchers
			[
				["launch_Titan_short_F",1]
			],
			// colonel launchers
			[
			]
		],
		[
			// private pistols
			[
			],
			// corporal pistols
			[
				["hgun_P07_F",1]
			],
			// sergeant pistols
			[
			],
			// lieutenant pistols
			[
			],
			// captain pistols
			[
				["hgun_PDW2000_F",1]
			],
			// major pistols
			[
				[if (productVersion select 3 >= 111471) then {"hgun_Pistol_heavy_01_F"} else {"hgun_PDW2000_F"},1]
			],
			// colonel pistols
			[
			]
		],
		[
			// private items
			[
			],
			// corporal items
			[
				["optic_Holosight",1, true],["acc_pointer_IR",1, true]
			],
			// sergeant items
			[
				["acc_flashlight",1, true],["optic_Holosight_smg",1, true]
			],
			// lieutenant items
			[
				["muzzle_snds_B",1, true],["muzzle_snds_H",1, true],["muzzle_snds_H_MG",1, true],["muzzle_snds_L",1, true], ["optic_MRCO",1, true]
			],
			// captain items
			[
				["optic_Aco",1, true],["optic_ACO_grn",1, true],["optic_Arco",1, true],["optic_Hamr",1, true], [if (productVersion select 3 >= 111471) then {"optic_MRD"} else {"optic_Hamr"},1, true]
			],
			// major items
			[
				["LaserDesignator",1], ["optic_Aco_smg",1], ["optic_ACO_grn_smg",1], ["optic_tws",1], ["optic_tws_mg",1], [if (productVersion select 3 >= 111471) then {"optic_DMS"} else {"optic_tws_mg"},1]
			],
			// colonel items
			[
				["optic_SOS",1], ["optic_NVS",1], ["optic_Nightstalker",1]
			]
		]
	];
	_x_ranked_weapons_e = [
		[
			// private rifles
			[
				["AK_74",5],["AK_47_M",5],["AK_47_S",5],["AKS_74",5],["AKS_74_U",5]
			],
			// corporal rifles (gets added to private rifles)
			[
				["AK_74_GL",5]
			],
			// sergeant rifles (gets added to corporal and private rifles)
			[
				["AK_74_GL_kobra",5],["AKS_74_kobra",5]
			],
			// lieutenant rifles (gets added to...)
			[
				["AKS_74_NSPU",5],["AKS_74_GOSHAWK",5]
			],
			// captain rifles (gets added...)
			[
				["AKS_74_pso",5]
			],
			// major rifles (gets...)
			[
			],
			// colonel rifles (...)
			[
			]
		],
		[
			// private sniper rifles
			[
			],
			// corporal sniper rifles
			[
			],
			// sergeant sniper rifles
			[
			],
			// lieutenant sniper rifles
			[
			],
			// captain sniper rifles
			[
				["SVD",5],["SVD_des_EP1",5]
			],
			// major sniper rifles
			[
				["SVD_NSPU_EP1",5]
			],
			// colonel sniper rifles
			[
				["KSVK",5]
			]
		],
		[
			// private MG
			[
			],
			// corporal MG
			[
				["RPK_74",5]
			],
			// sergeant MG
			[
				["PK",5]
			],
			// lieutenant MG
			[
			],
			// captain MG
			[
			],
			// major MG
			[
			],
			// colonel MG
			[
			]
		],
		[
			// private launchers
			[
				["RPG7V",5]
			],
			// corporal launchers
			[
				["STRELA",5],["RPG18",5]
			],
			// sergeant launchers
			[
				["Igla",5]
			],
			// lieutenant launchers
			[
			],
			// capain launchers
			[
				["MetisLauncher",5]
			],
			// major launchers
			[
			],
			// colonel launchers
			[
				["JAVELIN",2]
			]
		],
		[
			// private pistols
			[
				["Makarov",5]
			],
			// corporal pistols
			[
				["MakarovSD",5]
			],
			// sergeant pistols
			[
			],
			// lieutenant pistols
			[
			],
			// captain pistols
			[
			],
			// major pistols
			[
			],
			// colonel pistols
			[
			]
		],
		[
			// private items
			[
			],
			// corporal items
			[
				["optic_Holosight",1, true],["acc_pointer_IR",1, true]
			],
			// sergeant items
			[
				["acc_flashlight",1, true]
			],
			// lieutenant items
			[
				["muzzle_snds_B",1, true],["muzzle_snds_H",1, true],["muzzle_snds_H_MG",1, true],["muzzle_snds_L",1, true],["muzzle_snds_acp",1, true]
			],
			// captain items
			[
				["optic_Aco",1, true],["optic_ACO_grn",1, true],["optic_Arco",1, true],["optic_Hamr",1, true]
			],
			// major items
			[
			],
			// colonel items
			[
				["LaserDesignator",1]
			]
		]
	];
	
	{GVAR(misc_store) setVariable [_x,[]]} forEach ["PRIVATE","CORPORAL","SERGEANT","LIEUTENANT","CAPTAIN","MAJOR","COLONEL"];
	
	{
		for "_i" from 0 to (count _x) - 1 do {
			_typear = _x select _i;
			if (count _typear > 0) then {
				_rank = switch (_i) do {
					case 0: {
						{
							_ar = GV2(GVAR(misc_store),_x);
							{
								_ar set [count _ar, toUpper(_x select 0)];
								if (GVAR(WithAcre)) then {
									_ar set [count _ar, "ACRE_PRC119"];
								};
							} forEach _typear;
						} forEach ["PRIVATE","CORPORAL","SERGEANT","LIEUTENANT","CAPTAIN","MAJOR","COLONEL"];
					};
					case 1: {
						{
							_ar = GV2(GVAR(misc_store),_x);
							{_ar set [count _ar, toUpper(_x select 0)]} forEach _typear;
						} forEach ["CORPORAL","SERGEANT","LIEUTENANT","CAPTAIN","MAJOR","COLONEL"];
					};
					case 2: {
						{
							_ar = GV2(GVAR(misc_store),_x);
							{_ar set [count _ar, toUpper(_x select 0)]} forEach _typear;
						} forEach ["SERGEANT","LIEUTENANT","CAPTAIN","MAJOR","COLONEL"];
					};
					case 3: {
						{
							_ar = GV2(GVAR(misc_store),_x);
							{_ar set [count _ar, toUpper(_x select 0)]} forEach _typear;
						} forEach ["LIEUTENANT","CAPTAIN","MAJOR","COLONEL"];
					};
					case 4: {
						{
							_ar = GV2(GVAR(misc_store),_x);
							{_ar set [count _ar, toUpper(_x select 0)]} forEach _typear;
						} forEach ["CAPTAIN","MAJOR","COLONEL"];
					};
					case 5: {
						{
							_ar = GV2(GVAR(misc_store),_x);
							{_ar set [count _ar, toUpper(_x select 0)]} forEach _typear;
						} forEach ["MAJOR","COLONEL"];
					};
					case 6: {
						_ar = GV(GVAR(misc_store),COLONEL);
						{_ar set [count _ar, toUpper(_x select 0)]} forEach _typear;
					};
				};
			};
		};
	} forEach (_x_ranked_weapons_w + _x_ranked_weapons_e);
	
	x_ranked_weapons = if (GVAR(player_side) == blufor) then {_x_ranked_weapons_w} else {_x_ranked_weapons_e};
	_x_ranked_weapons_w = nil;
	_x_ranked_weapons_e = nil;
	
	execFSM "fsms\LimitWeaponsRanked.fsm";
};

[_vec] spawn {
	scriptName "spawn_x_weaponcargor_oa1";
	private ["_vec", "_old_rank", "_index", "_weapons", "_i", "_rk"];
	PARAMS_1(_vec);
	_old_rank = "";
	while {!isNull _vec && {alive _vec}} do {
		if (_old_rank != rank player) then {
			clearMagazineCargo _vec;
			clearWeaponCargo _vec;
			_old_rank = rank player;
			_index = _old_rank call FUNC(GetRankIndex);
			if (GVAR(player_side) == blufor) then {
				{
					_weapons = _x;
					for "_i" from 0 to _index do {
						_rk = _weapons select _i;
						{
							if (count _x == 2) then {
								_vec addweaponcargo _x
							} else {
								_vec addItemCargo [_x select 0, _x select 1];
							};
						} forEach _rk;
					};
				} forEach x_ranked_weapons;
				
				__awc(MineDetector,1)
				__awc(Binocular,1)
				if (GVAR(without_nvg) == 1) then {
					__aic(NVGoggles,1)
				};
				if (GVAR(no_faks) == 1) then {
					__aic(FirstAidKit,5)
				};
				__aic(H_Cap_blu,1)
				__aic(H_Cap_headphones,1)
				__aic(H_Cap_red,1)
				__aic(H_HelmetB,1)
				__aic(H_Booniehat_khk,1)
				__aic(H_HelmetB_paint,1)
				__aic(H_HelmetB_light,1)
				__aic(H_MilCap_mcamo,1)
				if (GVAR(pilots_only) == 1) then {
					__aic(H_PilotHelmetHeli_B,1)
				} else {
					if (count GVAR(only_pilots_can_fly) == 0) then {
						__aic(H_PilotHelmetHeli_B,1)
					} else {
						if (str(player) in GVAR(only_pilots_can_fly)) then {
							__aic(H_PilotHelmetHeli_B,1)
						};
					};
				};
				__aic(Medikit,5)
				__aic(ToolKit,1)
				__aic(B_UavTerminal,1)
				
				__aic(U_B_CombatUniform_mcam,1)
				__aic(U_B_CombatUniform_mcam_tshirt,1)
				__aic(U_B_CombatUniform_mcam_vest,1)
				__aic(U_B_GhillieSuit,1)
				if (GVAR(pilots_only) == 1) then {
					__aic(U_B_HeliPilotCoveralls,1)
				} else {
					if (count GVAR(only_pilots_can_fly) == 0) then {
						__aic(U_B_HeliPilotCoveralls,1)
					} else {
						if (str(player) in GVAR(only_pilots_can_fly)) then {
							__aic(U_B_HeliPilotCoveralls,1)
						};
					};
				};
				__aic(U_B_Wetsuit,1)
				__aic(V_RebreatherB,1)
				__aic(V_Rangemaster_belt,1)
				__aic(V_BandollierB_khk,1)
				__aic(V_BandollierB_cbr,1)
				__aic(V_BandollierB_rgr,1)
				//__aic(V_BandollierB_blk,1)
				__aic(V_PlateCarrier1_rgr,1)
				__aic(V_PlateCarrier1_rgr,1)
				//__aic(V_PlateCarrier1_cbr,1)
				__aic(V_PlateCarrier2_rgr,1)
				__aic(V_PlateCarrierGL_rgr,1)
				__aic(V_TacVest_khk,1)
				__aic(V_TacVest_brn,1)
				__aic(V_TacVest_oli,1)
				__aic(V_TacVest_blk,1)
				__aic(U_B_CombatUniform_mcam_worn,1)
				__aic(U_B_CombatUniform_wdl,1)
				__aic(U_B_CombatUniform_wdl_tshirt,1)
				__aic(U_B_CombatUniform_wdl_vest,1)
				__aic(U_B_CombatUniform_sgg,1)
				__aic(U_B_CombatUniform_sgg_tshirt,1)
				__aic(U_B_CombatUniform_sgg_vest,1)
				__aic(U_B_SpecopsUniform_sgg,1)
				
				__amc(30Rnd_45ACP_Mag_SMG_01,10)
				if (productVersion select 3 >= 111471) then {
				__amc(11Rnd_45ACP_Mag,10)
				};
				__amc(16Rnd_9x21_Mag,10)
				__amc(30Rnd_9x21_Mag,10)
				__amc(30Rnd_65x39_caseless_mag,10)
				__amc(30Rnd_65x39_caseless_mag_Tracer,10)
				__amc(30Rnd_65x39_caseless_mag,10)
				__amc(100Rnd_65x39_caseless_mag,10)

				__amc(20Rnd_762x51_Mag,10)
				__amc(20Rnd_556x45_UW_mag,10)
				__amc(30Rnd_556x45_Stanag,10)
				
				__amc(7Rnd_408_mag,3)
				
				__amc(200Rnd_65x39_cased_Box,10)
				__amc(200Rnd_65x39_cased_Box_Tracer,10)
				__amc(100Rnd_65x39_caseless_mag_Tracer,10)
				
				__amc(NLAW_F,3)
				__amc(Titan_AT,1)
				__amc(RPG32_F,3)
				__amc(Titan_AA,3)
				if (productVersion select 2 > 74) then {
					__amc(RPG32_HE_F,3)
				} else {
					__amc(RPG32_AA_F,3)
				};
				
				__amc(1Rnd_HE_Grenade_shell,10)
				__amc(1Rnd_Smoke_Grenade_shell,10)
				__amc(1Rnd_SmokeBlue_Grenade_shell,10)
				__amc(1Rnd_SmokeGreen_Grenade_shell,10)
				__amc(1Rnd_SmokeOrange_Grenade_shell,10)
				__amc(1Rnd_SmokePurple_Grenade_shell,10)
				__amc(1Rnd_SmokeRed_Grenade_shell,10)
				__amc(1Rnd_SmokeYellow_Grenade_shell,10)
				__amc(3Rnd_HE_Grenade_shell,10)
				__amc(3Rnd_Smoke_Grenade_shell,10)
				__amc(3Rnd_SmokeBlue_Grenade_shell,10)
				__amc(3Rnd_SmokeGreen_Grenade_shell,10)
				__amc(3Rnd_SmokeOrange_Grenade_shell,10)
				__amc(3Rnd_SmokePurple_Grenade_shell,10)
				__amc(3Rnd_SmokeRed_Grenade_shell,10)
				__amc(3Rnd_SmokeYellow_Grenade_shell,10)
				__amc(3Rnd_UGL_FlareCIR_F,10)
				__amc(3Rnd_UGL_FlareGreen_F,10)
				__amc(3Rnd_UGL_FlareRed_F,10)
				__amc(3Rnd_UGL_FlareWhite_F,10)
				__amc(3Rnd_UGL_FlareYellow_F,10)
				__amc(UGL_FlareCIR_F,10)
				__amc(UGL_FlareGreen_F,10)
				__amc(UGL_FlareRed_F,10)
				__amc(UGL_FlareWhite_F,10)
				__amc(UGL_FlareYellow_F,10)
				
				__amc(DemoCharge_Remote_Mag,5)
				__amc(SatchelCharge_Remote_Mag,5)
				__amc(ATMine_Range_Mag,5)
				__amc(ClaymoreDirectionalMine_Remote_Mag,5)
				__amc(APERSMine_Range_Mag,5)
				__amc(APERSBoundingMine_Range_Mag,5)
				__amc(SLAMDirectionalMine_Wire_Mag,5)
				__amc(APERSTripMine_Wire_Mag,5)
				
				__amc(HandGrenade,5)
				__amc(MiniGrenade,5)
				__amc(SmokeShell,5)
				__amc(SmokeShellYellow,5)
				__amc(SmokeShellGreen,5)
				__amc(SmokeShellRed,5)
				__amc(SmokeShellPurple,5)
				__amc(SmokeShellOrange,5)
				__amc(SmokeShellBlue,5)
				
				__amc(Chemlight_green,5)
				__amc(Chemlight_red,5)
				__amc(Chemlight_yellow,5)
				__amc(Chemlight_blue,5)
				__amc(Chemlight_blue,5)
				if (productVersion select 2 > 74) then {
					__amc(RPG32_HE_F,3)
				} else {
					__amc(RPG32_AA_F,3)
				};
				
				__amc(1Rnd_HE_Grenade_shell,10)
				__amc(1Rnd_Smoke_Grenade_shell,10)
				__amc(1Rnd_SmokeBlue_Grenade_shell,10)
				__amc(1Rnd_SmokeGreen_Grenade_shell,10)
				__amc(1Rnd_SmokeOrange_Grenade_shell,10)
				__amc(1Rnd_SmokePurple_Grenade_shell,10)
				__amc(1Rnd_SmokeRed_Grenade_shell,10)
				__amc(1Rnd_SmokeYellow_Grenade_shell,10)
				__amc(3Rnd_HE_Grenade_shell,10)
				__amc(3Rnd_Smoke_Grenade_shell,10)
				__amc(3Rnd_SmokeBlue_Grenade_shell,10)
				__amc(3Rnd_SmokeGreen_Grenade_shell,10)
				__amc(3Rnd_SmokeOrange_Grenade_shell,10)
				__amc(3Rnd_SmokePurple_Grenade_shell,10)
				__amc(3Rnd_SmokeRed_Grenade_shell,10)
				__amc(3Rnd_SmokeYellow_Grenade_shell,10)
				__amc(3Rnd_UGL_FlareCIR_F,10)
				__amc(3Rnd_UGL_FlareGreen_F,10)
				__amc(3Rnd_UGL_FlareRed_F,10)
				__amc(3Rnd_UGL_FlareWhite_F,10)
				__amc(3Rnd_UGL_FlareYellow_F,10)
				__amc(UGL_FlareCIR_F,10)
				__amc(UGL_FlareGreen_F,10)
				__amc(UGL_FlareRed_F,10)
				__amc(UGL_FlareWhite_F,10)
				__amc(UGL_FlareYellow_F,10)
				
				__amc(DemoCharge_Remote_Mag,5)
				__amc(SatchelCharge_Remote_Mag,5)
				__amc(ATMine_Range_Mag,5)
				__amc(ClaymoreDirectionalMine_Remote_Mag,5)
				__amc(APERSMine_Range_Mag,5)
				__amc(APERSBoundingMine_Range_Mag,5)
				__amc(SLAMDirectionalMine_Wire_Mag,5)
				__amc(APERSTripMine_Wire_Mag,5)
				
				__amc(Laserbatteries,5)
				
				__amc(HandGrenade,5)
				__amc(MiniGrenade,5)
				__amc(SmokeShell,5)
				__amc(SmokeShellYellow,5)
				__amc(SmokeShellGreen,5)
				__amc(SmokeShellRed,5)
				__amc(SmokeShellPurple,5)
				__amc(SmokeShellOrange,5)
				__amc(SmokeShellBlue,5)
				
				__amc(Chemlight_green,5)
				__amc(Chemlight_red,5)
				__amc(Chemlight_yellow,5)
				__amc(Chemlight_blue,5)
				__amc(Chemlight_blue,5)
			} else {
				{
					_weapons = _x;
					for "_i" from 0 to _index do {
						_rk = _weapons select _i;
						{_vec addweaponcargo _x} forEach _rk;
					};
				} forEach x_ranked_weapons;

				__awc(Binocular,1)
				__awc(Binocular_Vector,1)
				if (GVAR(without_nvg) == 1) then {
					__awc(NVGoggles,1)
				};
				
				__amc(30Rnd_545x39_AK,30)
				__amc(30Rnd_762x39_AK47,30)
				__amc(64Rnd_9x19_Bizon,30)
				__amc(64Rnd_9x19_SD_Bizon,30)
				__amc(8Rnd_B_Saiga12_74Slug,30)
				__amc(10Rnd_9x39_SP5_VSS,30)
				__amc(20Rnd_9x39_SP5_VSS,30)
				__amc(75Rnd_545x39_RPK,30)
				__amc(60Rnd_762x54_DT,50)
				__amc(30Rnd_9x19_UZI,50)
				__amc(30Rnd_9x19_UZI_SD,50)
				__amc(FlareWhite_GP25,20)
				__amc(FlareGreen_GP25,20)
				__amc(FlareRed_GP25,20)
				__amc(FlareYellow_GP25,20)
				__amc(1Rnd_HE_GP25,30)
				__amc(1Rnd_SMOKE_GP25,20)
				__amc(1Rnd_SmokeRed_GP25,20)
				__amc(1Rnd_SmokeGreen_GP25,20)
				__amc(1Rnd_SmokeYellow_GP25,20)
				__amc(30Rnd_545x39_AKSD,30)
				__amc(100Rnd_762x54_PK,30)
				__amc(10Rnd_762x54_SVD,6)
				__amc(8Rnd_9x18_Makarov,30)
				__amc(8Rnd_9x18_MakarovSD,30)
				__amc(PG7V,3)
				__amc(PG7VR,3)
				__amc(PG7VL,3)
				__amc(PG9_AT,3)
				__amc(OG7,3)
				__amc(OG9_HE,3)
				__amc(AT13,3)
				__amc(RPG18,3)
				__amc(Igla,5)
				__amc(SmokeShellRed,20)
				__amc(SmokeShellGreen,20)
				__amc(SmokeShellYellow,20)
				__amc(SmokeShellPurple,50)
				__amc(SmokeShellBlue,50)
				__amc(SmokeShellOrange,20)
				__amc(SmokeShell,20)
				__amc(HandGrenade_East,30)
				__amc(5Rnd_127x108_KSVK,3)
				__amc(Mine,10)
				__amc(Pipebomb,5)
				__amc(Laserbatteries,10)
				__amc(Strela,2)
				__amc(IR_Strobe_Target,3)
				__amc(IR_Strobe_Marker,3)
				__amc(IRStrobe,3)
			};
			if (GVAR(WithAcre)) then {
				__awc(ACRE_PRC119,1)
			};
		};
		sleep 2.32;
	};
};